package com.class1group3.demo.service.Impl;

import com.class1group3.demo.mapper.BooksMapper;
import com.class1group3.demo.pojo.Books;
import com.class1group3.demo.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BooksServiceImpl implements BooksService {
    @Autowired
    BooksMapper booksMapper;
    @Override
    public Books getBooksById(Integer id) {
        System.out.println(id);
        Books books = booksMapper.getBooksById(id);
        if (books == null){
            System.out.println("null");
        }else {
            System.out.println(books.getId());
            System.out.println(books.getName());
            System.out.println(books.getAuthors());
            System.out.println(books.getIsbn());
        }
        return books;
    }
    @Override
    public Map<String, Object> getBooksByIdAsMap(Integer id) {
        return booksMapper.getBooksByIdAsMap(id);
    }


    private int extractCount(String ratingDist) {
        return Integer.parseInt(ratingDist.split(":")[1]);
    }
    @Override
    public List<Map<String, Object>> groupByRatingDistribution(){return booksMapper.groupByRatingDistribution();}
    @Override
    public List<Map<String, Object>> groupByAuthorsStats(){return booksMapper.groupByAuthorsStats();}
    @Override
    public List<Map<String, Object>> selectYearlyBookCount(){ return booksMapper.selectYearlyBookCount();}
//    public Map<String, Object> calculateYearlyStats() {
//        Map<Integer, Integer> yearlyCounts = booksMapper.countBooksByYear();
//        Map<String, Object> result = new LinkedHashMap<>();
//
//        // 计算年平均增长率
//        if (yearlyCounts.size() >= 2) {
//            List<Integer> years = new ArrayList<>(yearlyCounts.keySet());
//            Collections.sort(years);
//
//            double totalGrowth = 0;
//            for (int i = 1; i < years.size(); i++) {
//                int prevCount = yearlyCounts.get(years.get(i-1));
//                int currentCount = yearlyCounts.get(years.get(i));
//                double growthRate = (currentCount - prevCount) * 100.0 / prevCount;
//                totalGrowth += growthRate;
//            }
//            double avgGrowthRate = totalGrowth / (years.size() - 1);
//            result.put("averageGrowthRate", String.format("%.2f%%", avgGrowthRate));
//        }
//
//        result.put("yearlyCounts", yearlyCounts);
//        return result;
//    }
    @Override
    public List< Map<String, Object>> getBooksByOrder(Books books) {
    //List< Map<String, Object>> list = booksMapper.getBooksByOrder(books);
    return null;
    }

    @Override
    public List<Map<String, Object>> countBooksByRating() {
        return booksMapper.countBooksByRating();

    }

    @Override
    public List<Map<String, Object>> countHotPublisher() {
        return booksMapper.countHotPublisher();
    }
}
